package cn.edu.scau.cmi.wangjushen.client;

import cn.edu.scau.cmi.wangjushen.multipleton.Marshal;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class MultipletonClient extends Application{
	Label resultLabel1 = new Label();
	Label resultLabel2 = new Label();
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		GridPane pane = new GridPane();
		Button creaButton = new Button("create");
		TextField inputField = new TextField();
		creaButton.setOnAction(e-> showResult(inputField.getText()));
		pane.setAlignment(Pos.CENTER);
		pane.setPadding(new Insets(12,12,12,12));
		pane.setVgap(10);
		pane.setHgap(10);
		pane.add(new Label("Marshal's Name:"), 0, 0);
		pane.add(inputField, 1, 0);
		pane.add(creaButton, 1, 1);
		pane.add(resultLabel1, 1, 2);
		pane.add(resultLabel2, 1, 3);
		Scene scene = new Scene(pane, 600, 200);
		primaryStage.setTitle("Mr.Marshal");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	
	private void showResult(String name) {
		Marshal marshal = Marshal.getInstance(name);
		String objectString;
		StringBuilder stringBuilder = new StringBuilder();
		for(Marshal marshal2 : Marshal.GetInstanceList()) {
			stringBuilder.append(marshal2.getName() + " " + marshal2.toString() + "\n");
		}
		objectString = stringBuilder.toString();
		resultLabel1.setText(objectString);
		if(marshal == null) {
			resultLabel2.setText("不能再创建了！");
		}
		
	}
	
	public static void main(String[] args) {
		Application.launch(args);
	}


}
